package com.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.dao.redis.IRedislLoginVerifyDao;
import com.dao.redis.redisEntity.LoginVerify;
import com.net.filter.LoginFilter;

/**
 * 一般的工具类
 * 
 * @author 尚业
 *
 */
public class Utils {
	private static Logger logger = Logger.getLogger(LoginFilter.class);

	/**
	 * 序列化
	 * 
	 * @param object
	 * @return
	 */
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 反序列化
	 * 
	 * @param bytes
	 * @return
	 */
	public static Object deserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 获取应用程序配置文件里的键值对返回
	 * 
	 * @return
	 */
	public static Properties getApplicationProperties() {
		InputStream inputStream = Utils.class
				.getResourceAsStream("/properties/application.properties");
		Properties p = new Properties();
		if (inputStream == null) {
			logger.error("空的");
			return null;
		}
		try {
			p.load(inputStream);
		} catch (IOException e1) {
			logger.error("加载属性异常", e1);
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				logger.error("关闭流异常", e);
			}
		}
		return p;
	}

	/**
	 * 删除目录或文件，如果是目录包含子目录
	 * 
	 * @param path
	 * @return
	 */
	public static boolean deleteAllFilesOfDir(File path) {
		if (!path.exists())
			return true;
		if (path.isFile()) {
			if (path.delete())
				return true;
			else 
				return false;
		}
		File[] files = path.listFiles();
		for (int i = 0; i < files.length; i++) {
			deleteAllFilesOfDir(files[i]);
		}
		if (path.delete())
			return true;
		else {
			return false;
		}
	}

	/**
	 * 通过api请求添加的token从内存数据库中获取用户名
	 * 
	 * @param redislLoginVerifyDao
	 *            操作验证登录的类
	 * @param token
	 * @return
	 */
	public static String getUserNameBytaken(
			IRedislLoginVerifyDao redislLoginVerifyDao, String token) {
		LoginVerify loginVerify = redislLoginVerifyDao.get(token);
		if (null == loginVerify)
			return null;
		else
			return loginVerify.getUserName();
	}
	
	public static String getName1Bytaken(
			IRedislLoginVerifyDao redislLoginVerifyDao, String token) {
		LoginVerify loginVerify = redislLoginVerifyDao.get(token);
		if (null == loginVerify)
			return null;
		else
			return loginVerify.getName1();
	}
	public static String getShopnameBytaken(
			IRedislLoginVerifyDao redislLoginVerifyDao, String token) {
		LoginVerify loginVerify = redislLoginVerifyDao.get(token);
		if (null == loginVerify)
			return null;
		else
			return loginVerify.getShopname();
	}
}
